﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Javu2D
{
    public delegate void CollectionOperation<T>(T item);
    public class JavuCollection<T> where T : class
    {
        public List<T> Item = new List<T>();
        public Dictionary<string, T> ItemMap=new Dictionary<string,T>();
        
        public JavuCollection()
        {

        }
        public T Last
        {
            get
            {
                return Item[Item.Count - 1];
            }
        }
        public T First
        {
            get
            {
                return Item[0];
            }
        }
        public T Remove(string id)
        {
            var item = ItemMap[id];
            ItemMap.Remove(id);
            Item.Remove(item);
            return item;
        }
        public T Remove(int index)
        {
            T item = Item[index];
            Item.Remove(item);
            foreach (var key in ItemMap.Keys)
            {
                if (ItemMap[key].Equals(item))
                {
                    ItemMap.Remove(key);
                    return item;
                }
            }
            return item;
        }
        public bool HasItems
        {
            get
            {
                return Item.Count > 0 ? true : false;
            }
        }
        public void ForEach(CollectionOperation<T> op)
        {
            Item.ForEach((T item) => { op(item); });
        }
        public void ForEachMT(CollectionOperation<T> op,ThreadPriority priority = ThreadPriority.BelowNormal)
        {
            Item.ForEach((item) =>
            {
                Thread th = new Thread(() => { op(item); });
                th.Priority = priority;
                th.Start();
            });
         
        }
        public bool Contains(T item)
        {
            return ItemMap.ContainsValue(item);
        }
        public bool Contains(string id)
        {
            return ItemMap.ContainsKey(id);
        }
        public void PushFirst(T item,string id="none")
        {
            if (Item.Count == 0)
            {
                Item.Add(item);
            }
            else
            {
                Item.Insert(0, item);
            }
            if (id == "none")
            {
                id = (Item.Count + 1).ToString();
            }
            ItemMap.Add(id, item);
        }
        public void PushLast(T item, string id = "none")
        {
            Item.Add(item);
            if (id == "none")
            {
                id = (Item.Count + 1).ToString();
            }
            ItemMap.Add(id, item);

        }
        public T PopFirst()
        {
            var item = Item[0];
            Item.RemoveRange(0, 1);
            return item;
        }
        public T PopLast()
        {
            var item = Item[Item.Count - 1];
            Item.RemoveRange(Item.Count - 1, 1);
            return item;
        }
        public T PopID(string id)
        {
            var item = ItemMap[id];
            ItemMap.Remove(id);
            Item.Remove(item);
            return item;
        }
     
    

    }
}
